package com.controller;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.UserDao;
import com.dao.UserRoleDao;
import com.pojo.User;

@Controller
public class UserController {

	@Resource
	private UserDao uDao;
	@Resource
	private UserRoleDao urDao;

	@RequestMapping(value = "/getUserList")
	public String getUserList(Model model) {
		Collection<User> user = uDao.getUserList();
		// List<User> user = dao.getUserList();
		System.out.println(user);
		model.addAttribute("user", user);
		System.out.println(1111111);
		return "UserManeger";
	}

	@RequestMapping(value = "/updateOne")
	public String updateOne(int id, Model model) {
		System.out.println(id);
		User userOne = new User();
		userOne = uDao.userUpdateOne(id);
		System.out.println(userOne);
		model.addAttribute("userOne", userOne);
		System.out.println(444444);
		return "upadateOne";

	}

	@PostMapping(value = "/update")
	public String update(User user) {
		System.out.println(user);
		int up = uDao.update(user);
		System.out.println(up);
		return "forward:/getUserList";

	}

	@RequestMapping(value = "showAllUser")
	public String getAll(Model model, HttpSession session) {
		Subject subject = SecurityUtils.getSubject();
		boolean flag1 = subject.hasRole("admin");
		if (flag1) {
			List<User> list =uDao.getAll();
			model.addAttribute("Users", list);
			return "showUser";
		}
		return "index";
	}

	@ResponseBody
	@RequestMapping(value="getOneUser",method = RequestMethod.POST)
	public User getOne(int id){
		User user =uDao.getOneUser(id);
		return user;
	}

	@ResponseBody
	@RequestMapping(value = "delOneUser")
	public String delOne(int id) {
		boolean b = uDao.delOne(id);
		try {
			if (b) {
				return "ok";
			} else {
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}

	@ResponseBody
	@RequestMapping(value = "addOneUser", method = RequestMethod.POST)
	public String addOne(User user, int r_id) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = dateFormat.format(date);
		user.setU_jointime(time);
		uDao.addOne(user);
		boolean b = urDao.addRole(r_id, user.getU_id());
		try {
			if (b) {
				return "ok";
			} else {
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}

	@ResponseBody
	@RequestMapping(value="updateOneUser", method = RequestMethod.POST)
	public String updateOne(User user,int r_id){
		urDao.updateRole(r_id,user.getU_id());
		boolean b= uDao.updateOne(user);
		try{
            if (b){
                return "ok";
            }
            else {
                return "failed";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "failed";
        }
	}
	@RequestMapping(value = "findOne/{nickname}")
	public String find(Model model, @PathVariable String nickname) {
		System.out.println("nickname:" + nickname);
		List<User> list = uDao.selectAllByNickname(nickname);
		model.addAttribute("Users", list);
		return "showUser";
	}
}
